//
//  UpdatePasswordViewController.m
//  JimiPay
//
//  Created by yull on 16/1/9.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "UpdatePasswordViewController.h"

@interface UpdatePasswordViewController ()

@end

@implementation UpdatePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"修改登陆密码";
    [self setupCustomBackItem];
}

- (IBAction)button_commitUpdate:(id)sender {
    
    [self.view endEditing:YES];
    
    NSString *loginPwd = [self.updatePasswordTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *againLoginPwd = [self.confirmTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (loginPwd.length < 6) {
        [self.view makeToast:@"密码不能小于6位" duration:2.0f position:@"center"];
        return;
    }
    if (![loginPwd isEqualToString:againLoginPwd]) {
        [self.view makeToast:@"两次输入密码不一致" duration:2.0f position:@"center"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestForUpdatePwdWithPwdType:@"1"
                                      updateType:@"1"
                                           value:[self.oldPasswordTF.text md5]
                                          newPwd:[self.updatePasswordTF.text md5]
                                      custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE]
                                        complete:^(BOOL succeed, NSString *msg, NSString *cod) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if (succeed) {
            [self.view makeToast:msg duration:2.0f position:@"center"];
            [self performSelector:@selector(popToLogin) withObject:self afterDelay:2.0f];
        }
        else{
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

- (void)popToLogin
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
